<?php
/**
 * 账单组和用户关系表
 * @author hero
 */
class GroupUserModel extends BaseModel {

	public static $name='group_user';
	public static $primary='gu_id';
	
	public function __construct() {
		parent::__construct(CoreFactory::db(),self::$name,self::$primary);
	}
	
	/**
	 * 关联用户和组
	 * @author solar
	 * @param int $group_id
	 * @param int $user_id
	 * @param int $is_default
	 * @return void
	 */
	public function create($group_id, $user_id, $is_default) {
		$row['g_id'] = $group_id;
		$row['u_id'] = $user_id;
		$row['is_default'] = $is_default;
		return $this->insert($row);
	}
	
	/**
	 * 移除账单组成员
	 * @author solar
	 * @param int $group_id
	 * @param int $user_id
	 * @return int
	 */
	public function delete($group_id,$user_id) {
		return $this->removeByField(array('g_id'=>$group_id,'u_id'=>$user_id));
	}
	
	/**
	 * 判断是否用户是否已加入该组
	 * @author solar
	 * @param int $user_id
	 * @param int $group_id
	 * @return boolean
	 */
	public function inGroup($user_id,$group_id) {
		$row=$this->getByField(array('u_id'=>$user_id,'g_id'=>$group_id));
		return empty($row) ? false : true;
	}
	
	/**
	 * 获取账单组的用户ID数组
	 * @param int $group_id
	 */
	public function getUserIDs($group_id) {
		$IDs=array();
		$sql='SELECT u_id FROM `group_user` WHERE g_id='.$group_id;
		$list=$this->db->fetchAll($sql);
		foreach($list as $row) $IDs[]=$row['u_id'];
		return $IDs;
	}
	
	/**
	 * 账单组成员列表
	 * @author solar
	 * @param int $group_id
	 * @return list
	 */
	public function listGroupMember($group_id) {
		$sql='SELECT gu.*,u.sina_id,u.u_user_name,u.u_real_name,u.u_image_url FROM `group_user` AS gu
		 LEFT JOIN `user` AS u ON gu.u_id=u.u_id WHERE gu.g_id='.$group_id.' ORDER BY gu.gu_id';
		return $this->db->fetchAll($sql);
	}
	
	/**
	 * 账单组成员键值
	 * @author solar
	 * @param int $group_id
	 * @return array
	 */
	public function getMemberKV($group_id) {
		$KV = array();
		$sql='SELECT u.u_id,u.u_user_name FROM `group_user` AS gu
		 LEFT JOIN `user` AS u ON gu.u_id=u.u_id WHERE gu.g_id='.$group_id;
		$list = $this->db->fetchAll($sql);
		foreach($list as $row) $KV[$row['u_id']] = $row['u_user_name'];
		return $KV;
	}
	
	/**
	 * 屏蔽账单组用户
	 * @author solar
	 * @param $group_id
	 * @return int
	 */	
	public function inactiveUsers($group_id) {
		return $this->updateByField(array('g_status'=>0), array('g_id'=>$group_id));
	}
	
	/**
	 * 获取默认群组ID
	 * @author solar
	 * @param int $user_id
	 * @return int
	 */
	public function getDefaultGroup($user_id) {
		$aField = array('u_id'=>$user_id, 'g_status'=>1,'is_default'=>1);
		return $this->getOneByField($aField, 'g_id');
	}
	
	/**
	 * 获取默认群组行
	 * @author solar
	 * @param int $user_id
	 * @return array
	 */
	public function getDefaultRow($user_id) {
		$aField = array('u_id'=>$user_id, 'g_status'=>1,'is_default'=>1);
		return $this->getByField($aField);
	}
	
	/**
	 * 设置默认群组
	 * @author solar
	 * @param int $user_id
	 * @param int $group_id
	 * @return int
	 */
	public function setDefaultGroup($user_id, $group_id) {
		$aField = array('g_id'=>$group_id, 'u_id'=>$user_id, 'g_status'=>1);
		return $this->updateByField(array('is_default'=>1), $aField);
	}
	
	/**
	 * 设置随机默认群组
	 * @author solar
	 * @param int $user_id
	 */
	public function setRandomDefault($user_id) {
		$sql = 'UPDATE `group_user` SET is_default=1 WHERE u_id='.$user_id.' AND g_status=1 ORDER BY is_default DESC limit 1;';
		$this->db->query($sql);
	}
		
	/**
	 * 拥有的群组数量
	 * @author solar
	 * @param int user_id
	 * @return int
	 */
	public function countGroup($user_id) {
		return $this->getCount(array('u_id'=>$user_id, 'g_status'=>1));
	}
	
}

?>